home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_13_08 / phillip2 / rtiff.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-05-11  |  6.4 KB  |  228 lines

  1.  
  2.        /*********************************************
  3.        *
  4.        *  file d:\cips\rtiff.c
  5.        *
  6.        *  Functions: This file contains
  7.        *          read_tiff_image
  8.        *          read_line
  9.        *          seek_to_first_line
  10.        *          seek_to_end_of_line
  11.        *
  12.        *  Purpose:
  13.        *  These functions read a TIFF image and insert
  14.        *  the data into a ROWSxCOLS array of short.
  15.        *
  16.        *       NOTE: The fseek constants are
  17.        *             0=SEEK_SET = beginning of file
  18.        *             1=SEEK_CUR = current offset
  19.        *             2=SEEK_END = end of file
  20.        *
  21.        *  External Calls:
  22.        *  tiff.c - read_tiff_header
  23.        *
  24.        *  Modifications:
  25.        *       25 June 1990 - created
  26.        *       27 March 1993 - use fopen, fread, fseek
  27.        *           instead of the earlier open, read,
  28.        *           seek, etc.
  29.        *
  30.        **********************************************/
  31.  
  32. #include "cips.h"
  33.  
  34.  
  35.  
  36.  
  37.  
  38. read_tiff_image(image_file_name, array, il, ie, ll, le)
  39.       char   image_file_name[];
  40.       int    il, ie, ll, le;
  41.       short   array[ROWS][COLS];
  42. {
  43.    char  buffer[COLS],
  44.          rep[80];
  45.    int   bytes_read,
  46.          closed,
  47.          position,
  48.          i;
  49.    FILE  *image_file;
  50.    float a;
  51.    long  line_length, offset;
  52.  
  53.    struct tiff_header_struct image_header;
  54.  
  55.    read_tiff_header(image_file_name, &image_header);
  56.  
  57.       /***********************************************
  58.       *
  59.       *   Procedure:
  60.       *   Seek to the strip offset where the data begins.
  61.       *   Seek to the first line you want.
  62.       *   Loop over the lines you want to read:
  63.       *      Seek to the first element of the line.
  64.       *      Read the line.
  65.       *      Seek to the end of the data in that line.
  66.       *
  67.       ************************************************/
  68.  
  69.    image_file = fopen(image_file_name, "rb");
  70.    if(image_file != NULL){
  71.       position = fseek(image_file, 
  72.                        image_header.strip_offset, 
  73.                        SEEK_SET);
  74.       position = seek_to_first_line(image_file, 
  75.                                     &image_header, il);
  76.  
  77.       for(i=0; i<(ll-il); i++){
  78.          offset       = (ie-1)/
  79.                         (8/image_header.bits_per_pixel);
  80.          position     = fseek(image_file, offset, 
  81.                               SEEK_CUR);
  82.          bytes_read   = read_line(image_file, array, 
  83.                                   i, &image_header, 
  84.                                   ie, le);
  85.          position     = seek_to_end_of_line(image_file, 
  86.                                  le, &image_header);
  87.          position     = fseek(image_file, 1, 
  88.                               SEEK_CUR); 
  89.       }  /* ends loop over i  */
  90.  
  91.       closed = fclose(image_file);
  92.    }  /* ends if file opened ok */
  93.    else{
  94.       printf("\nRTIFF.C> ERROR - cannot open "
  95.              "tiff file");
  96.    }
  97.  
  98. }  /*  ends read_tiff_image */
  99.  
  100.  
  101.  
  102.  
  103.        /**********************************************
  104.        *
  105.        *   read_line(...
  106.        *
  107.        *   This function reads bytes from the TIFF 
  108.        *   file into a buffer, extracts the numbers 
  109.        *   from that buffer, and puts them into a 
  110.        *   ROWSxCOLS array of shorts. The process 
  111.        *   depends on the number of bits per pixel used 
  112.        *   in the file (4 or 8).
  113.        *
  114.        **********************************************/
  115.  
  116. read_line(image_file, array, line_number, 
  117.           image_header, ie, le)
  118.    FILE   *image_file;
  119.    int    ie, le, line_number;
  120.    short  array[ROWS][COLS];
  121.    struct tiff_header_struct *image_header;
  122. {
  123.    char  buffer[COLS], first, second;
  124.    float a, b;
  125.    int bytes_read, i;
  126.    unsigned int bytes_to_read;
  127.    union short_char_union scu;
  128.  
  129.    for(i=0; i<COLS; i++)
  130.       buffer[i] = '\0';
  131.  
  132.         /********************************************
  133.         *
  134.         *   Use the number of bits per pixel to 
  135.         *   calculate how many bytes to read.
  136.         *
  137.         ********************************************/
  138.  
  139.    bytes_to_read = (le-ie)/
  140.                    (8/image_header->bits_per_pixel);
  141.    bytes_read    = fread(buffer, 1, bytes_to_read, 
  142.                          image_file);
  143.  
  144.    for(i=0; i<bytes_read; i++){
  145.  
  146.         /*********************************************
  147.         *
  148.         *   Use unions defined in cips.h to stuff bytes
  149.         *   into shorts.
  150.         *
  151.         **********************************************/
  152.  
  153.       if(image_header->bits_per_pixel == 8){
  154.        scu.s_num          = 0;
  155.        scu.s_alpha[0]        = buffer[i];
  156.        array[line_number][i] = scu.s_num;
  157.       }  /* ends if bits_per_pixel == 8 */
  158.  
  159.       if(image_header->bits_per_pixel == 4){
  160.  
  161.        scu.s_num             = 0;
  162.        second                = buffer[i] & 0X000F;
  163.        scu.s_alpha[0]        = second;
  164.        array[line_number][i*2+1] = scu.s_num;
  165.  
  166.        scu.s_num             = 0;
  167.        first                 = buffer[i] >> 4;
  168.        first                 = first & 0x000F;
  169.        scu.s_alpha[0]        = first;
  170.        array[line_number][i*2] = scu.s_num;
  171.  
  172.       }  /* ends if bits_per_pixel == 4 */
  173.  
  174.    }  /*  ends loop over i  */
  175.  
  176.    return(bytes_read);
  177.  
  178. }  /* ends read_line  */
  179.  
  180.  
  181.  
  182.  
  183.  
  184.        /*********************************************
  185.        *
  186.        *   seek_to_first_line(...
  187.        *
  188.        **********************************************/
  189.  
  190. seek_to_first_line(image_file, image_header, il)
  191.    FILE   *image_file;
  192.    int    il;
  193.    struct tiff_header_struct *image_header;
  194. {
  195.    long offset;
  196.    int  position;
  197.  
  198.    offset   = (il-1)*image_header->image_width/
  199.              (8/image_header->bits_per_pixel);
  200.       /* seek from current position */
  201.    position = fseek(image_file, offset, SEEK_CUR);
  202.    return(position);
  203. }  /* ends seek_to_first_line */
  204.  
  205.  
  206.  
  207.  
  208.  
  209.        /**********************************************
  210.        *
  211.        *   seek_to_end_of_line(...
  212.        *
  213.        ***********************************************/
  214.  
  215. seek_to_end_of_line(image_file, le, image_header)
  216.    FILE   *image_file;
  217.    int    le;
  218.    struct tiff_header_struct *image_header;
  219. {
  220.    long  offset;
  221.    int   position;
  222.  
  223.    offset   = (image_header->image_width-le)/
  224.              (8/image_header->bits_per_pixel);
  225.    position = fseek(image_file, offset, SEEK_CUR);
  226.    return(position);
  227. }  /* ends seek_to_end_of_line         */
  228.